package org.assertj.ohos.api.bluetooth;

import ohos.bluetooth.BluetoothRemoteDevice;
import org.assertj.core.api.AbstractAssert;
import static ohos.bluetooth.BluetoothRemoteDevice.*;
import static org.assertj.ohos.internal.BitmaskUtils.buildNamedValueString;
import static org.assertj.core.api.Assertions.assertThat;

public class BluetoothDeviceAssert extends AbstractAssert<BluetoothDeviceAssert, BluetoothRemoteDevice> {
  public BluetoothDeviceAssert(BluetoothRemoteDevice actual) {
    super(actual, BluetoothDeviceAssert.class);
  }

  public BluetoothDeviceAssert hasAddress(String address) {
    isNotNull();
    String actualAddress = actual.getDeviceAddr();
    assertThat(actualAddress) //
        .overridingErrorMessage("Expected address <%s> but was <%s>.", address, actualAddress) //
        .isEqualTo(address);
    return this;
  }

  public BluetoothDeviceAssert hasBondState(int state) {
    isNotNull();
    int actualState = actual.getPairState();
    //noinspection ResourceType
    assertThat(actualState) //
        .overridingErrorMessage("Expected bond state <%s> but was <%s>.", bondStateToString(state),
            bondStateToString(actualState)) //
        .isEqualTo(state);
    return this;
  }

  public BluetoothDeviceAssert hasName(String name) {
    isNotNull();
    String actualName = actual.getDeviceName().get();
    assertThat(actualName) //
        .overridingErrorMessage("Expected name <%s> but was <%s>.", name, actualName) //
        .isEqualTo(name);
    return this;
  }


  public BluetoothDeviceAssert hasType(int type) {
    isNotNull();
    int actualType = actual.getDeviceType();
    //noinspection ResourceType
    assertThat(actualType) //
        .overridingErrorMessage("Expected type <%s> but was <%s>.", typeToString(type),
            typeToString(actualType)) //
        .isEqualTo(type);
    return this;
  }

  public static String bondStateToString(int state) {
    return buildNamedValueString(state)
        .value(PAIR_STATE_NONE, "none")
        .value(PAIR_STATE_PAIRED, "bonded")
        .value(PAIR_STATE_PAIRING, "bonding")
        .get();
  }

  public static String typeToString(int type) {
    return buildNamedValueString(type) //
        .value(TYPE_CLASSIC, "classic")
        .value(TYPE_DUAL, "dual")
        .value(TYPE_LE, "le")
        .value(TYPE_UNKNOWN, "unknown")
        .get();
  }
}
